package com.airS.task.fileMgr.service.gk.impl;

import com.airS.task.fileMgr.common.page.Page;
import com.airS.task.fileMgr.example.gk.RolePermissionExample;
import com.airS.task.fileMgr.mapper.gk.RolePermissionMapper;
import com.airS.task.fileMgr.model.gk.Permission;
import com.airS.task.fileMgr.model.gk.Role;
import com.airS.task.fileMgr.model.gk.RolePermission;
import com.airS.task.fileMgr.query.gk.RolePermissionQuery;
import com.airS.task.fileMgr.service.gk.PermissionService;
import com.airS.task.fileMgr.service.gk.RolePermissionService;
import com.airS.task.fileMgr.service.gk.RoleService;
import com.airS.task.fileMgr.utils.page.PageUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by alan on 2017/3/14.
 */
@Service
public class RolePermissionServiceImpl implements RolePermissionService {

    @Autowired
    private RolePermissionMapper rolePermissionMapper;

    @Autowired
    private RoleService roleService;

    @Autowired
    private PermissionService permissionService;

    @Override
    public RolePermission queryRolePermissionById(Integer id) {
        return rolePermissionMapper.selectByPrimaryKey(id);
    }

    @Override
    public Page<RolePermission> queryRolePermissions(RolePermissionQuery query) {
        PageUtil.startPage(query);
        List<RolePermission> rolePermissions = rolePermissionMapper.selectByExample(assemblyRolePermissionExample(query));
        Map<Integer, Role> roles = roleService.getAllRoleToMap();
        Map<Integer, Permission> permissions = permissionService.getAllPermissionToMap();
        for(RolePermission rolePermission : rolePermissions){
            Role role = roles.get(rolePermission.getRoleId());
            Permission permission = permissions.get(rolePermission.getPermissionId());
            rolePermission.setRoleName(role == null ? "未定义" : role.getRoleName());
            rolePermission.setPermissionName(permission  == null ? "未定义" : permission.getName());
        }
        return new Page<RolePermission>(rolePermissions);
    }

    @Override
    public Integer addRolePermission(RolePermission rolePermission) {
        rolePermissionMapper.insert(rolePermission);
        return rolePermission.getRolePermissionId();
    }

    @Override
    public Integer updateRolePermissionById(RolePermission rolePermission) {
        return rolePermissionMapper.updateByPrimaryKeySelective(rolePermission);
    }

    @Override
    public Integer delRolePermissionById(Integer id) {
        return rolePermissionMapper.deleteByPrimaryKey(id);
    }

    @Override
    public Integer delRolePermissionByIds(List<Integer> ids) {
        RolePermissionExample example = new RolePermissionExample();
        RolePermissionExample.Criteria criteria = example.createCriteria();
        criteria.andRolePermissionIdIn(ids);
        return rolePermissionMapper.deleteByExample(example);
    }

    private RolePermissionExample assemblyRolePermissionExample(RolePermissionQuery query){
        RolePermissionExample example = new RolePermissionExample();
        RolePermissionExample.Criteria criteria = example.createCriteria();
        if(query.getRoleId() != null){
            criteria.andRoleIdEqualTo(query.getRoleId());
        }
        if(query.getPermissionId() != null){
            criteria.andPermissionIdEqualTo(query.getPermissionId());
        }
        example.setOrderByClause("create_time desc");
        return example;
    }
}
